package com.byzk.invoice.xml;

import com.byzk.invoice.util.DocumentUtil;
import org.dom4j.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * OFD.xml.
 *
 * @author 康鹏 2020-05-20
 */
public class OFD {

    /**
     * 创建OFD.xml.
     * @param OFDXmlPath OFD.xml存放路径
     * @throws Exception Exception
     */
    public static void createOFDXml(String OFDXmlPath) throws Exception {

        Namespace namespace = new Namespace("ofd", "http://www.ofdspec.org/2016");
        //创建外层<ofd:OFD />
        Element rootElt = DocumentHelper.createElement(new QName("OFD", namespace));
        rootElt.addAttribute("DocType", "OFD");
        rootElt.addAttribute("Version", "1.1");
        //创建<ofd:DocBody />
        Element docBodyElt = rootElt.addElement("ofd:DocBody");
        //创建<ofd:DocInfo />
        Element docInfoElt = docBodyElt.addElement("ofd:DocInfo");
        //创建<ofd:DocID />
        Element docIdElt = docInfoElt.addElement("ofd:DocID");
        docIdElt.addText("32位的编号");
        //创建<ofd:CreationDate />
        Element creationDateElt = docInfoElt.addElement("ofd:CreationDate");
        creationDateElt.addText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        //创建<ofd:CustomDatas />
        Element customDatasElt = docInfoElt.addElement("ofd:CustomDatas");
        //创建<ofd:CustomData />
        Element customDataElt = customDatasElt.addElement("ofd:CustomData");
        customDataElt.addAttribute("Name", "provider");
        customDataElt.setText("BYZK");
        //创建<ofd:DocRoot />
        Element docRootElt = docBodyElt.addElement("ofd:DocRoot");
        docRootElt.addText("Doc_0/Document.xml");

        Document document = DocumentHelper.createDocument(rootElt);
        DocumentUtil.writer(document, OFDXmlPath + File.separator + "OFD.xml", true);
    }

}
